package ch.bfh.mobicomp.soda2.gps_datacollector.business.listener;

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.widget.Toast;
import ch.bfh.mobicomp.soda2.gps_datacollector.business.utils.SODA_GPS_DataFileWriter;
import ch.bfh.mobicomp.soda2.gps_datacollector.business.utils.SODA_GPS_SharedPreferenceManager;
import ch.bfh.mobicomp.soda2.gps_datacollector.presentation.SODA_GPS_MainActivity;

public class SODA_GPS_LocationListener implements LocationListener {

    private static final String TAG = SODA_GPS_LocationListener.class.getName();

    public static final String ALTITUDE = "ALTITUDE";
    public static final String LONGITUDE = "LONGITUDE";
    public static final String LATITUDE = "LATITUDE";
    public static final String TIME = "TIME";

    private Intent intent;
    private Service service;
    private Time time;

    private SODA_GPS_DataFileWriter sODA_GPS_DataFileWriter;

    public SODA_GPS_LocationListener(Service service) {
        this.time = new Time();
        this.service = service;
        this.intent = new Intent(SODA_GPS_MainActivity.UI_BROADCAST);
        this.sODA_GPS_DataFileWriter = new SODA_GPS_DataFileWriter();
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d(TAG, "onLocationChanged: " + location);
        Log.d(TAG, "Latitude: " + location.getLatitude());
        Log.d(TAG, "Longitude: " + location.getLongitude());
        String latitude = location.getLatitude() + "";
        String longitude = location.getLongitude() + "";
        String altitude = location.getAltitude() + "";
        time.setToNow();
        String timestamp = time.format2445();
        putLastPositionToSharedPrefs(timestamp, latitude, longitude, altitude);
        putExtrasToIntent(timestamp, latitude, longitude, altitude);
        sODA_GPS_DataFileWriter.writetoFile(timestamp, latitude, longitude, altitude);

    }

    private void putLastPositionToSharedPrefs(String timestamp, String latitude, String longitude, String altitude) {
        SODA_GPS_SharedPreferenceManager preferenceManager = SODA_GPS_SharedPreferenceManager.getInstance(service);
        preferenceManager.saveLastDate(timestamp, latitude, longitude, altitude);
        Log.d(TAG, "Saved the Last Position in Shared Prefs");
    }

    private void putExtrasToIntent(String time, String latitude, String longitude, String altitude) {
        intent.putExtra(LATITUDE, latitude);
        intent.putExtra(LONGITUDE, longitude);
        intent.putExtra(ALTITUDE, altitude);
        intent.putExtra(TIME, time);
        intent.setAction("SODA_GPS");
        service.sendBroadcast(intent);
        Log.d(TAG, "Send UI Broadcast");
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d(TAG, "onProviderDisabled: " + provider);
        intent.putExtra("MESSAGE", "NO PROVIDER: " + provider + " --> [GPS Ischaute du Depp]");
        service.sendBroadcast(intent);
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d(TAG, "onProviderEnabled: " + provider);

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d(TAG, "onStatusChanged Provider: " + provider);
        Log.d(TAG, "onStatusChanged Status: " + status);
        Log.d(TAG, "onStatusChanged Extras: " + extras);
        Toast.makeText(service, "Status changed:" + status + " : " + provider + " : " + extras, Toast.LENGTH_LONG)
                .show();
    }

}
